<?php
class RP_Model_User_Jobseeker extends RP_Model_User_Base{
	
	private $name;
	
	private $curr_location;
	
	private $mobile_no;
	
	private $alternate_no;
	
	private $exp_years;
	
	private $exp_months;
	
	private $skills;
	
	private $resume_headline;
	
	private $industry_id;
	
	private $industry_name;
	
	private $fa_id;
	
	private $fa_name;
	
	private $course_type;
	
	private $course_id;
	
	private $course_name;
	
	private $edu_spec_id;
	
	private $edu_spec_name;
	
	private $edu_org_id;
	
	private $edu_org_name;
	
	private $edu_style;
	
	private $year_of_passing;
	
	private $mapper;
	
	private $ug_check;
	
	private $highest_qualification;	
	
	/**
	 * Overridden function getUser_type_id();
	 * @return the $user_type_id 
	 */
	public function getUser_type_id() {
		if(!isset($this->_user_type_id)){
			$this->setUser_type_id('SEKR');
		}
		return $this->_user_type_id;
	}
	
	/**
	 * @return the $name
	 */
	public function getName() {
		return $this->name;
	}

	/**
	 * @return the $curr_location
	 */
	public function getCurr_location() {
		return $this->curr_location;
	}

	/**
	 * @return the $mobile_no
	 */
	public function getMobile_no() {
		return $this->mobile_no;
	}

	/**
	 * @return the $alternate_no
	 */
	public function getAlternate_no() {
		return $this->alternate_no;
	}

	/**
	 * @return the $exp_years
	 */
	public function getExp_years() {
		return $this->exp_years;
	}

	/**
	 * @return the $exp_months
	 */
	public function getExp_months() {
		return $this->exp_months;
	}

	/**
	 * @return the $skills
	 */
	public function getSkills() {
		return $this->skills;
	}

	/**
	 * @return the $resume_headline
	 */
	public function getResume_headline() {
		return $this->resume_headline;
	}

	/**
	 * @return the $indusrty_id
	 */
	public function getIndustry_id() {
		return $this->industry_id;
	}

	/**
	 * @return the $industry_name
	 */
	public function getIndustry_name() {
		return $this->industry_name;
	}

	/**
	 * @return the $fa_id
	 */
	public function getFa_id() {
		return $this->fa_id;
	}

	/**
	 * @return the $fa_name
	 */
	public function getFa_name() {
		return $this->fa_name;
	}

	/**
	 * @return the $course_type
	 */
	public function getCourse_type() {
		return $this->course_type;
	}

	/**
	 * @return the $course_id
	 */
	public function getCourse_id() {
		return $this->course_id;
	}

	/**
	 * @return the $course_name
	 */
	public function getCourse_name() {
		return $this->course_name;
	}

	/**
	 * @return the $edu_spec_id
	 */
	public function getEdu_spec_id() {
		return $this->edu_spec_id;
	}

	/**
	 * @return the $edu_spec_name
	 */
	public function getEdu_spec_name() {
		return $this->edu_spec_name;
	}

	/**
	 * @return the $edu_org_id
	 */
	public function getEdu_org_id() {
		return $this->edu_org_id;
	}

	/**
	 * @return the $edu_org_name
	 */
	public function getEdu_org_name() {
		return $this->edu_org_name;
	}

	/**
	 * @return the $edu_style
	 */
	public function getEdu_style() {
		return $this->edu_style;
	}

	/**
	 * @return the $year_of_passing
	 */
	public function getYear_of_passing() {
		return $this->year_of_passing;
	}
	
	/**
	 * @return the $ug_check
	 */
	public function getUg_check() {
		return $this->ug_check;
	}
	
	/**
	 * @return the $highest_qualification
	 */
	public function getHighest_qualification() {
		return $this->highest_qualification;
	}
	
	/**
	 * @return the $mapper
	 */
	public function getMapper()
    {
        if (null === $this->mapper) {
            $this->setMapper(new RP_Model_User_JobseekerMapper());
        }
        return $this->mapper;
    }
	
		/**
	 * @param field_type $name
	 */
	public function setName($name) {
		$this->name = $name;
		return $this;
	}

	/**
	 * @param field_type $curr_location
	 */
	public function setCurr_location($curr_location) {
		$this->curr_location = $curr_location;
		return $this;
	}

	/**
	 * @param field_type $mobile_no
	 */
	public function setMobile_no($mobile_no) {
		$this->mobile_no = $mobile_no;
		return $this;
	}

	/**
	 * @param field_type $alternate_no
	 */
	public function setAlternate_no($alternate_no) {
		$this->alternate_no = $alternate_no;
		return $this;
	}

	/**
	 * @param field_type $exp_years
	 */
	public function setExp_years($exp_years) {
		$this->exp_years = $exp_years;
		return $this;
	}

	/**
	 * @param field_type $exp_months
	 */
	public function setExp_months($exp_months) {
		$this->exp_months = $exp_months;
		return $this;
	}

	/**
	 * @param field_type $skills
	 */
	public function setSkills($skills) {
		$this->skills = $skills;
		return $this;
	}

	/**
	 * @param field_type $resume_headline
	 */
	public function setResume_headline($resume_headline) {
		$this->resume_headline = $resume_headline;
		return $this;
	}

	/**
	 * @param field_type $industry_id
	 */
	public function setIndustry_id($industry_id) {
		$this->industry_id = $industry_id;
		return $this;
	}

	/**
	 * @param field_type $industry_name
	 */
	public function setIndustry_name($industry_name) {
		$this->industry_name = $industry_name;
		return $this;
	}

	/**
	 * @param field_type $fa_id
	 */
	public function setFa_id($fa_id) {
		$this->fa_id = $fa_id;
		return $this;
	}

	/**
	 * @param field_type $fa_name
	 */
	public function setFa_name($fa_name) {
		$this->fa_name = $fa_name;
		return $this;
	}

	/**
	 * @param field_type $course_type
	 */
	public function setCourse_type($course_type) {
		$this->course_type = $course_type;
		return $this;
	}

	/**
	 * @param field_type $course_id
	 */
	public function setCourse_id($course_id) {
		$this->course_id = $course_id;
		return $this;
	}

	/**
	 * @param field_type $course_name
	 */
	public function setCourse_name($course_name) {
		$this->course_name = $course_name;
		return $this;
	}

	/**
	 * @param field_type $edu_spec_id
	 */
	public function setEdu_spec_id($edu_spec_id) {
		$this->edu_spec_id = $edu_spec_id;
		return $this;
	}

	/**
	 * @param field_type $edu_spec_name
	 */
	public function setEdu_spec_name($edu_spec_name) {
		$this->edu_spec_name = $edu_spec_name;
		return $this;
	}

	/**
	 * @param field_type $edu_org_id
	 */
	public function setEdu_org_id($edu_org_id) {
		$this->edu_org_id = $edu_org_id;
		return $this;
	}

	/**
	 * @param field_type $edu_org_name
	 */
	public function setEdu_org_name($edu_org_name) {
		$this->edu_org_name = $edu_org_name;
		return $this;
	}

	/**
	 * @param field_type $edu_style
	 */
	public function setEdu_style($edu_style) {
		$this->edu_style = $edu_style;
		return $this;
	}

	/**
	 * @param field_type $year_of_passing
	 */
	public function setYear_of_passing($year_of_passing) {
		$this->year_of_passing = $year_of_passing;
		return $this;
	}

	/**
	 * @param field_type $mapper
	 */
	public function setMapper($mapper) {
		$this->mapper = $mapper;
		return $this;
	}
	
	/**
	 * @param field_type $ug_check
	 */
	public function setUg_check($ug_check) {
		$this->ug_check = $ug_check;
		return $this;
	}
	

	/**
	 * @param field_type $highest_qualification
	 */
	public function setHighest_qualification($highest_qualification) {
		$this->highest_qualification = $highest_qualification;
		return $this;
	}
	
	
	public function __construct (array $options = null)
    {
        if (is_array($options)) {
            $this->setOptions($options);
            $this->setUser_type_id('SEKR');
        }
        /*
        if (isset($options['agent_reg_id'])) {
            self::isValid();
        }*/
    }
    
    public function __set ($name, $value)
    {
        $method = 'set' . ucfirst($name);
        if ('mapper' == $name || ! method_exists($this, $method)) {
            throw new Exception('Invalid property specified', Zend_Log::DEBUG);
        }
        $this->$method($value);
    }
    
    public function __get ($name)
    {
        $method = 'get' . ucfirst($name);
        if ('mapper' == $name || ! method_exists($this, $method)) {
            throw new Exception('Invalid property specified. :'.$method, Zend_Log::DEBUG);
        }
        return $this->$method();
    }
    
    public function setOptions (array $options)
    {
        $methods = get_class_methods($this);
        foreach ($options as $key => $value) {
            $method = 'set' . ucfirst($key);
            if (in_array($method, $methods)) {
                $this->$method($value);
            }
        }
        return $this;
    }
    
    public function register(){
    	
    	$return = $this->getMapper()->register($this);
    	if ($return) {
            if ($return instanceof RP_Model_User_Jobseeker) {
            return true;
        	}
        return null;
    	}	
    }
    
    protected function _populateSeekerPersonal(){
    	$result = null;
    	$cols = array('name','mobile_no','alternate_no','addressline',
    				  'city','district','state','pincode','highest_qualification');
    	$result = Zend_Db_Table::getDefaultAdapter()
    					->select()
    					->from('seeker_personal',$cols)
    					->where('user_id = ?',$this->getUser_id())
    					->query()->fetch();
    	Zend_Registry::get('logger')->debug($result);
    	if($result!=null){
    		$this->setName($result['name']);
    		if(isset($result['mobile_no'])){
    			$this->setMobile_no($result['mobile_no']);
    		}
    		if(isset($result['alternate_no'])){
    			$this->setAlternate_no($result['alternate_no']);
    		}
    		if(isset($result['highest_qualification'])){
    			$this->setHighest_qualification($result['highest_qualification']);
    		}
    		
    		return true;
    	}
    	return false;
    }
    
    protected function _populateSeekerProfessional(){
    	$result = null;
    	$bind = array($this->getUser_id());
    	$sql = 'SELECT
				   sp.user_id
				    ,sp.industry_id
				    , sp.fa_id
				    , sp.curr_location
				    , sp.exp_months
				    , sp.exp_years
				    , sp.skills
				    , sp.resume_headline
				    , sp.curr_designation
				    , sp.curr_employer
				    , sp.curr_job_profile
				    , sp.summary
				    , i.industry_name
				    , f.fa_name
				FROM
				    seeker_professional sp
				    INNER JOIN fa f 
				        ON (sp.fa_id = f.fa_id)
				    INNER JOIN industry i 
				        ON (sp.industry_id = i.industry_id)
				        WHERE sp.user_id = ?';
    	$result = Zend_Db_Table::getDefaultAdapter()->query($sql,$bind)->fetch();
    	//Zend_Registry::get('logger')->debug($result);
    	if($result!=null){
    		
    		if(isset($result['industry_id'])){
    			$this->setIndustry_id($result['industry_id']);
    		}
    		if(isset($result['industry_name'])){
    			$this->setIndustry_name($result['industry_name']);
    		}
    		if(isset($result['fa_id'])){
    			$this->setFa_id($result['fa_id']);
    		}
    		if(isset($result['fa_name'])){
    			$this->setFa_name($result['fa_name']);
    		}
    		if(isset($result['curr_location'])){
    			$this->setCurr_location($result['curr_location']);
    		}
    		if(isset($result['exp_months'])){
    			$this->setExp_months($result['exp_months']);
    		}
    		if(isset($result['exp_years'])){
    			$this->setExp_years($result['exp_years']);
    		}
    		if(isset($result['skills'])){
    			$this->setSkills($result['skills']);
    		}
    		if(isset($result['resume_headline'])){
    			$this->setResume_headline($result['resume_headline']);
    		}    		
    		return true;
    	}
    	return false;
    }
    
    protected function _populateSeekerEducation(){
    	$result = null;
    	$bind = array($this->getUser_id());
    	$sql = 'SELECT
				    e.edu_spec_id
				    , e.course_id
				    , e.edu_org_id
				    , e.edu_style
				    , e.year
				    , es.edu_spec_name
				    , c.course_name
				    , eo.edu_org_name
				FROM
				    education e
				    INNER JOIN seeker_personal sp 
				        ON (e.user_id = sp.user_id)
				    INNER JOIN edu_spec es 
				        ON (e.edu_spec_id = es.edu_spec_id)
				    INNER JOIN edu_org eo 
				        ON (e.edu_org_id = eo.edu_org_id)
				    INNER JOIN course c 
				        ON (e.course_id = c.course_id)
				WHERE e.user_id = ?';
    	
    	$result = Zend_Db_Table::getDefaultAdapter()->query($sql,$bind)->fetch();
    	Zend_Registry::get('logger')->debug($result);
    	if($result!=null){
    		if(isset($result['edu_spec_id'])){
    			$this->setEdu_spec_id($result['edu_spec_id']);
    		}
    		if(isset($result['edu_spec_name'])){
    			$this->setEdu_spec_name($result['edu_spec_name']);
    		}
    		if(isset($result['course_id'])){
    			$this->setCourse_id($result['course_id']);
    		}
    		if(isset($result['course_name'])){
    			$this->setCourse_name($result['course_name']);
    		}
    		if(isset($result['edu_org_id'])){
    			$this->setEdu_org_id($result['edu_org_id']);
    		}
    		if(isset($result['edu_org_name'])){
    			$this->setEdu_org_name($result['edu_org_name']);
    		}
    		if(isset($result['edu_style'])){
    			$this->setEdu_style($result['edu_style']);
    		}
    		if(isset($result['year'])){
    			$this->setYear_of_passing($result['year']);
    		}
    		return true;
    	}
    	return false;
    }
    
    /**
     * (non-PHPdoc)
     * @see RP_Model_User_Base::populateUser()
     * Overridden fuunction of base class.
     */
    public function populateUser(){
    	if(parent::populateUser()){
    		$this->_populateSeekerPersonal();
    		$this->_populateSeekerProfessional();
    		$this->_populateSeekerEducation();
    		return true;
    	}
    	return false;
    }
    
    public function fetchForSearch($param){
    	$sql = 'SELECT 
				sp.name, sp.mobile_no, spo.exp_months, spo.exp_years, spo.skills, spo.exp_years, spo.exp_months, 
				spo.curr_location, f.fa_name, au.email, sp.highest_qualification, i.industry_name, es.edu_spec_name, c.course_name
				FROM seeker_personal sp
				INNER JOIN seeker_professional spo ON (sp.user_id = spo.user_id)
				INNER JOIN education e ON (e.user_id = sp.user_id)
				INNER JOIN edu_spec es ON (es.edu_spec_id = e.edu_spec_id)
				INNER JOIN course c ON (c.course_id = e.course_id)
				INNER JOIN fa f ON (f.fa_id = spo.fa_id)
				INNER JOIN industry i ON (i.industry_id = spo.industry_id)
				INNER JOIN auth_user au ON (au.user_id = sp.user_id) 
				WHERE spo.exp_years >= '.$param['exp_min'].' AND spo.exp_years <= '. $param['exp_max'];
    	
    	
    	if(isset($param['industry_id'])){
			$sql = $sql.' AND i.industry_id = '.$param['industry_id'];
		}
    	if(isset($param['course_id'])){
			$sql = $sql.' AND c.course_id = '.$param['course_id'];
		}
    	if(isset($param['edu_spec_id'])){
			$sql = $sql.' AND es.edu_spec_id = '.$param['edu_spec_id'];
		}
    	if(isset($param['course_type'])){
			$sql = $sql.' AND sp.highest_qualification = "'.$param['course_type'].'"';
		}
		$sql = $sql.' ORDER BY exp_years DESC ';
		//Zend_Registry::get('logger')->debug($sql);
			
    	$result = Zend_Db_Table::getDefaultAdapter()
    			  ->query($sql)->fetchAll();
//    	$spData = array('name','mobile_no','highest_qualification');
//    	$spoData = array('curr_location','exp_months','exp_years','skills');
//    	$faData = array('fa_name');
//    	$cData = array('course_name');
//    	$esData = array('edu_spec_name');
//    	$iData = array('industry_name');
//    	$auData = array('email');
//    	
//    	$dbTable = Zend_Db_Table::getDefaultAdapter()
//    				->select()
//    				->from('seekerpersonal',$spData)
//    				->join('seekerprofessional', 'seekerpersonal.user_id = seekerprofessional.user_id',$spoData) 
//    				->join('education','seekerpersonal.user_id = education.user_id')
//    				->join('edu_spec','education.edu_spec_id = edu_spec.edu_spec_id',$esData)
//    				->join('course','course.course_id = education.course_id',$cData)
//    				->join('fa','fa.fa_id = seekerprofessional.fa_id',$faData)
//    				->join('industry','industry.industry_id = seekerpersonal.industry_id',$iData)
//    				->join('auth_user', 'auth_user.user_id = seekerpersonal.user_id');
//    				
//    				
//    	if(isset($param['course_id'])){
//    		$dbTable = $dbTable->where('course.course_id = ?',$param['course_id']);
//    	}
//    	if(isset($param['industry_id'])){
//    		$dbTable = $dbTable->where('industry.industry_id = ?',$param['industry_id']);
//    	}
//    	if(isset($param['edu_spec_id'])){
//    		$dbTable = $dbTable->where('edu_spec.edu_spec_id = ?',$param['edu_spec_id']);
//    	}
//    	if(isset($param['course_type'])){
//    		$dbTable = $dbTable->where('sp.highest_qualification = ?',$param['course_type']);
//    	}
//    	
//    	$result = $dbTable->query()->fetchAll();
    	
    	return $result;
    }

}//End class
?>